package seekting.demo2017.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.seekting.demo_lib.MainActivity;

import seekting.demo2017.AppEnv;
import seekting.demo2017.R;

/**
 * Created by seekting on 17-5-18.
 */

public class MaskStrokeActivity extends Activity {

    public static final boolean DEBUG = AppEnv.bAppdebug;
    public static final String TAG = MaskStrokeActivity.class.getSimpleName();
    public static final PorterDuffXfermode XFERMODE = new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT);

    static {

        MainActivity.TITLE_MAP.put(MaskStrokeActivity.class, "");
        MainActivity.DESC_MAP.put(MaskStrokeActivity.class, "");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate.");
        }
        final PathEffect pathEffect = new DashPathEffect(new float[]{10f, 10f}, 0);
        final BlurMaskFilter blurFilter = new BlurMaskFilter(5,
                BlurMaskFilter.Blur.OUTER);
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.dip);
        final BitmapShader shader = new BitmapShader(bitmap1, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

        final BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.mask);
        final Paint paint = new Paint();
//        paint.setColor(Color.RED);
//        paint.setShader(shader);
//        paint.setMaskFilter(blurFilter);
        int offsetXY[] = new int[]{1, 1};
        final Bitmap bitmap = bitmapDrawable.getBitmap().extractAlpha(paint, offsetXY);
        final Rect rect = new Rect();
        rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final Rect rect1 = new Rect();
        int gap = Math.round(getResources().getDisplayMetrics().density);
        rect1.set(-gap, -gap, bitmap.getWidth() + gap, bitmap.getHeight() + gap);

        View v = new View(this) {
            @Override
            protected void onDraw(Canvas canvas) {
                drawStrok1(canvas);
            }

            private void drawStrok1(Canvas canvas) {
//                int count = canvas.saveLayer(0, 0, getWidth(), getHeight(), paint, Canvas.ALL_SAVE_FLAG);
//                paint.setXfermode(null);
//                paint.setShader(null);
//                paint.setMaskFilter(null);
//                canvas.drawBitmap(bitmap, rect, rect, paint);
//                paint.setXfermode(XFERMODE);
                paint.setShader(shader);
//                paint.setColor(Color.RED);
                paint.setMaskFilter(blurFilter);
                paint.setPathEffect(pathEffect);
                canvas.drawBitmap(bitmap, rect, rect, paint);
//                canvas.drawBitmap(bitmapDrawable.getBitmap(), 0, 0, paint);
//                canvas.drawRect(rect,paint);
//                canvas.restoreToCount(count);
                paint.setXfermode(null);
                paint.setShader(null);
                paint.setMaskFilter(null);
            }
        };
        v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        setContentView(v);

    }
}
